<script>
	export default {
		onLaunch: function () {
			console.log('App Launch')
			// #ifdef APP-PLUS
			//判断用户是否登录
			try {
				var token = uni.getStorageSync('token');
				console.log(token+'>>>>>>>>>>>>>>>>>>????')
			} catch (e) {
			
				//TODO handle the exception
			
			}
			
			if (token == '' || token == null) {
			
				// uni.showModal({
				//     title: "提示",
				//     content: "已阅读并同意遵守《用户协议》和《隐私政策》",
				//     showCancel: false,
				//     confirmText: "确定",
				//     confirmColor: "#00ffff",
				//     success(res) {
				//         if (res.confirm) {
				//             console.log('确认')                                   
				//         }
				//     }
				// })
			
			}
			
			var url = this.apiurl + "update"
			//console.log(url)
			let that = this;
			
			plus.runtime.getProperty(plus.runtime.appid, function(inf) {
			
				console.log(url);
			
				console.log('App Launch', inf.version);
				uni.request({
					url: url, //检查更新的服务器地址
					data: {
						appid: plus.runtime.appid,
						version: inf.version,
						imei: plus.device.imei
					},
					success: (res) => {
						console.log('rere', res)
						if (res.statusCode == 200 && res.data.isUpdate) {
							let openUrl = plus.os.name === 'iOS' ? res.data.iOS : res.data.Android;
			
							console.log("======================");
							console.log(res.data);
			
							// 提醒用户更新
							uni.showModal({
								title: '更新提示',
								showCancel: false,
								content: res.data.note ? res.data.note : '是否选择更新',
								success: (showResult) => {
									if (showResult.confirm) {
										that.downWgt(openUrl);
										// plus.runtime.openURL(openUrl);
									}
								}
							})
						}
					}
				})
				// uni.setStorageSync("_version",inf.version);
			
			})
			
			
			// 检测升级
			
			// #endif
			
		},
		onShow: function () {
			console.log('App Show');
			// #ifdef APP
			uni.setStorageSync('appediton', plus.runtime.version);
			// #endif
			
		},
		onHide: function () {
			console.log('App Hide')
		},
		
		methods: {
			check() {
				var url = this.apiurl + "update"
				//console.log(url)
				let that = this;
		
				plus.runtime.getProperty(plus.runtime.appid, function(inf) {
		
					console.log('App Launch', inf.version);
					uni.request({
						url: url, //检查更新的服务器地址
						data: {
							appid: plus.runtime.appid,
							version: inf.version,
							imei: plus.device.imei
						},
						success: (res) => {
							console.log('rere', res)
							if (res.statusCode == 200 && res.data.isUpdate) {
								let openUrl = plus.os.name === 'iOS' ? res.data.iOS : res.data.Android;
								// 提醒用户更新
								uni.showModal({
									title: '更新提示',
									showCancel: false,
									content: res.data.note ? res.data.note : '是否选择更新',
									success: (showResult) => {
										if (showResult.confirm) {
											that.downWgt(openUrl);
											// plus.runtime.openURL(openUrl);
										}
									}
								})
							}
						}
					})
					// uni.setStorageSync("_version",inf.version);
		
				})
			},
		
			downWgt(wgtUrl) {
				console.log('进来了吗')
				var that = this;
				//var text = "正在下载更新文件..text.";
				// var wgtUrl="https://dingding.keweitianhui.com/public/uploads/9F37CF2.wgt";
				//var text = plus.nativeUI.showWaiting("开始下载更新...");
				var text = plus.nativeUI.showWaiting("开始下载更新...");
		
				const dtask = plus.downloader.createDownload(wgtUrl, {
					filename: "_doc/update/"
				}, function(d, status) {
					if (status == 200) {
						console.log("下载wgt成功：" + d.filename);
						that.installWgt(d.filename); // 安装wgt包  
					} else {
						console.log("下载wgt失败！");
						plus.nativeUI.alert("下载wgt失败！");
					}
					plus.nativeUI.closeWaiting();
				})
		
				dtask.addEventListener("statechanged", function(task, status) {
					var nowData = (task.downloadedSize * 100 / task.totalSize);
					var nowTxt = nowData.toFixed(0);
		
					if (nowData > 1) {
						if (nowTxt % 1 == 0) { // 让百分比 1% 增长，如果这里不这么处理  出现 堆栈内存溢出的问题，有知道原因的大神指导一下哈
							text.setTitle("　　 已下载: " + nowTxt + "%　　 ");
						}
					}
		
				}, false);
		
				dtask.start();
			},
		
			installWgt(path) {
				console.log('进来了吗', path)
				plus.nativeUI.showWaiting("安装wgt文件...");
				plus.runtime.install(path, {
					force: true
				}, function() {
					plus.nativeUI.closeWaiting();
					console.log("安装wgt文件成功！");
					plus.nativeUI.alert("恭喜,更新已完成!", function() {
						plus.runtime.restart();
					});
				}, function(e) {
					plus.nativeUI.closeWaiting();
					console.log("安装wgt文件失败[" + e.code + "]：" + e.message);
					plus.nativeUI.alert("安装wgt文件失败[" + e.code + "]：" + e.message);
				});
			},
		},
	}
</script>

<style lang="scss">
	@import "@/uni_modules/uview-ui/index.scss";
	/*每个页面公共css */
	// @import "colorui/main.css";
	// @import "colorui/icon.css";

	@import "/static/iconfont/font.scss";
	
	@import "/static/css/common.scss";
</style>
